package org.test.device.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

import java.lang.annotation.Annotation;

/**
 * 〈使用websocket〉<br>
 *
 * @author Mr.Wang
 * @create 2025/9/29
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 配置消息代理，客户端订阅路径以"/topic"开头
        config.enableSimpleBroker("/topic");
        // 配置客户端发送消息的前缀
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册WebSocket端点，前端通过此端点连接
        registry.addEndpoint("/mqtt-websocket")
                .setAllowedOriginPatterns("*") // 允许跨域
                .withSockJS();
    }
}